package com.yehuishou.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.dao.AssessmentValueDao;
import com.yehuishou.dao.BidRecordDao;
import com.yehuishou.dao.BidingOrderDao;
import com.yehuishou.dao.FileDao;
import com.yehuishou.dao.LeaseItemDao;
import com.yehuishou.dao.SaleOrderDao;
import com.yehuishou.dao.SellItemDao;
import com.yehuishou.pojo.QueryVO;
import com.yehuishou.pojo.SellItemQueryParameters;
import com.yehuishou.pojo.ShoppingTrolley;
import com.yehuishou.pojo.Zldd;
import com.yehuishou.pojo.AssessmentValue;
import com.yehuishou.pojo.BidOrder;
import com.yehuishou.pojo.BidingOrder;
import com.yehuishou.pojo.Bidrecord;
import com.yehuishou.pojo.Brand;
import com.yehuishou.pojo.Classify;
import com.yehuishou.pojo.Configurations;
import com.yehuishou.pojo.EnterpriseBack;
import com.yehuishou.pojo.ImageFile;
import com.yehuishou.pojo.Item_order;
import com.yehuishou.pojo.Models;
import com.yehuishou.pojo.Oditem;
import com.yehuishou.pojo.Order;
import com.yehuishou.service.SaleOrderService;

/**
 * 用户订单业务实现层
 * @author VULCAN
 *
 */
@Service
public class SaleOrderServiceImpl implements SaleOrderService{

	@Autowired
	private SaleOrderDao saleOrderDao;

	@Autowired
	private SellItemDao sellitemdao;
	
	@Resource
	private AssessmentValueDao assessmentValueDao;
	
	@Autowired
	private LeaseItemDao lIdao;
	
	@Resource
	private BidRecordDao bidRecordDao;
		
	@Resource
	private FileDao fileDao;
	
	@Resource
	private BidingOrderDao bidingOrderDao;
	
	
	@Override
	public int deleteUser(QueryVO vo) {

		return saleOrderDao.deleteUser(vo);	
	}
	
	
	@Override
	public PageInfo<QueryVO> findUserOrder(Integer pageNum,Integer pageSize,QueryVO vo) {
		PageHelper.startPage(pageNum, pageSize);
		List<QueryVO> list =saleOrderDao.findUserOrder(vo);			
		PageInfo<QueryVO> mv =new PageInfo<QueryVO>(list);
		
		return mv;
	}

	
	//通过订单编号 查询订单下面的所有商品详情
	@Override
	public List<Oditem> finditems(String oid) {
		//查询商品详情
		List<Oditem> qu=saleOrderDao.finditems(oid);
		for (int i = 0; i <qu.size(); i++) {
			//通过循环遍历 获取每个商品详情的ID
			String img=qu.get(i).getImage();
			//查询订单详情里面是否带有图片
			if(img!=null && !img.equals("")) {
			//将详情里的图片 通过 ;号 进行分割 存到 字符串中
			String[] imgs=img.substring(1, img.length()).split(";");
				//将获得到的图片 信息 转化为 Integer 类型  
				Integer imgid=Integer.parseInt(imgs[0]);
				//查询图片地址 使用图片类接收
				ImageFile imagefile=saleOrderDao.selimg(imgid);
				//将图片地址放入到集合中
				qu.get(i).setImage(imagefile.getFilePath());
			}
		}
		return qu;
	}

	@Override
	public Integer UpdateDetails(Oditem oditem) {
		
		return saleOrderDao.UpdateDetails(oditem);
		
	}

	@Override
	public Integer detailsDelete(Integer i_id) {
		
		return saleOrderDao.detailsDelete(i_id);
		
	}

	@Override
	public Oditem FindDetailsById(Oditem oditem) {
		
			Oditem od = saleOrderDao.FindDetailsById(oditem);
			return od;

	}

	@Override
	public List<Classify> selclassify() {
		List<Classify> list = saleOrderDao.selclassify();
		return list;
	}
	
	@Override
	public List<Brand> selbrand() {
		List<Brand> brand = saleOrderDao.selbrand();
		return brand;
	}

	@Override
	public List<Models> selmodel() {
		List<Models> model = saleOrderDao.selmodel();
		return model;
	}

	@Override
	public Integer UpdateConfiguration(Oditem oditem) {
	
		return saleOrderDao.UpdateConfiguration(oditem);
	}
	
	/**
	 * 订单中间表
	 */
	@Override
	public int addOrderItem(Item_order item_order) {
		int i = saleOrderDao.addOrderItem(item_order);
		return i;
	}
	
	//添加到购物车
	@Override
	public int addOrderCat(ShoppingTrolley shopt) {
		// TODO Auto-generated method stub
		int i= saleOrderDao.addOrderCat(shopt);
		return i;
	}

	//添加订单
	@Override
	public int addOrder(Order order) {
		// TODO Auto-generated method stub
		int i=saleOrderDao.addOrder(order);
		return i;
	}
	
	//删除购物车数据
	@Override
	public int deleShoppingTrolley(Integer stidd) {
		// TODO Auto-generated method stub
		int i=saleOrderDao.deleShoppingTrolley(stidd);
		return i;
	}

	//查询支付订单
	@Override
	public List<Order> selpayorder(String oid) {
		// TODO Auto-generated method stub
		List<Order> order=saleOrderDao.selpayorder(oid);
		return order;
	}

	//查询个人所有订单
	@Override
	public Map<String, Object> selallorder(Integer uid, Integer ul, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<>();

		//购物车订单
		if(ul==1){
			PageHelper.startPage(pageNum, pageSize);
		}else{
			PageHelper.startPage(1, 5);
		}
		List<Order> shopt=saleOrderDao.selShoppingTrolley(uid);
		for (int i = 0; i < shopt.size(); i++) {
			String image=shopt.get(i).getImage();
			if (image != null && !"".equals(image)) {
				String[] imgs = image.substring(1, image.length()).split(";");
				Integer s = Integer.parseInt(imgs[0]);
				ImageFile imagefile = sellitemdao.selimg(s);
				shopt.get(i).setFilePath(imagefile.getFilePath());
			}
		}
		PageInfo<Order> pageinfoshopt=new PageInfo<>(shopt);
		
		
		//购买商品订单
		if(ul==2){
			PageHelper.startPage(pageNum, pageSize);
		}else{
			PageHelper.startPage(1, 5);
		}
		Integer order_type=2;
		List<Order> sellorder=saleOrderDao.selorder(uid,order_type);
		for (int i = 0; i < sellorder.size(); i++) {
			String oid=sellorder.get(i).getOid();
			
			List<Item_order> item_orders=saleOrderDao.selitem_order(oid);
			
			for (int j = 0; j < item_orders.size(); j++) {
				String image=item_orders.get(j).getImage();
				if (image != null && !"".equals(image)) {
					String[] imgs = image.substring(1, image.length()).split(";");
					Integer s = Integer.parseInt(imgs[0]);
					ImageFile imagefile = sellitemdao.selimg(s);
					item_orders.get(j).setFilePath(imagefile.getFilePath());
				}
			}
			sellorder.get(i).setItem_order(item_orders);
		}		
		PageInfo<Order> pageinfosellorder=new PageInfo<>(sellorder);
		
		//回收商品订单
		if(ul==3){
			PageHelper.startPage(pageNum, pageSize);
		}else{
			PageHelper.startPage(1, 5);
		}
		order_type=1;
		List<Order> backorder=saleOrderDao.selbackorder(uid,order_type);
		
		for (int i = 0; i < backorder.size(); i++) {
			String img_url=backorder.get(i).getImg_url();			
			if (img_url != null && !"".equals(img_url)) {
				String[] imgs = img_url.substring(1, img_url.length()).split(";");
				Integer s = Integer.parseInt(imgs[0]);
				ImageFile imagefile = sellitemdao.selimg(s);
				backorder.get(i).setFilePath(imagefile.getFilePath());
			}
			String back_assess=backorder.get(i).getBack_assess();
			String backassesss="";
			if (back_assess != null && !"".equals(back_assess)) {
				String[] back_assesss = back_assess.substring(1, back_assess.length()).split(";");
				for (int j = 0; j < back_assesss.length; j++) {
					Integer backassess = Integer.parseInt(back_assesss[j]);
					AssessmentValue as =assessmentValueDao.getAssessmentValueByvalid(backassess);
					backassesss+=(as.getAssess_value()+" ");
				}
			}
			backorder.get(i).setBack_assess(backassesss);
		}
		PageInfo<Order> pageinfobackorder=new PageInfo<>(backorder);
		
		//租赁商品
		if(ul==4){
			PageHelper.startPage(pageNum, pageSize);
		}else{
			PageHelper.startPage(1, 5);
		}
		List<Zldd> leaseorder = lIdao.findZldd(uid);
		PageInfo<Zldd> pageinfoleaseorder=new PageInfo<>(leaseorder);
		
		
		//竞价订单
		if(ul==5){
			PageHelper.startPage(pageNum, pageSize);
		}else{
			PageHelper.startPage(1, 5);
		}
	
		List<Bidrecord> biddingorder  = bidRecordDao.selectRecordByUid(uid);
		
		for(int i=0;i<biddingorder.size();i++) {
			
			Bidrecord bi =biddingorder.get(i);
			String img =bi.getImg_url();
			
			if(img!=null&&img!="") {
				
				String img_url[] =img.split(";");
				if(img_url.length>1) {
					
					bi.setImg_url(fileDao.getOneImage(Integer.parseInt(img_url[1])).getFilePath());
				}
			}
			
			Integer bid =bi.getBid();
			BidOrder or = new BidOrder();
			or.setBid(bid);
			or.setUid(bi.getUid());
			//根据用户id以及竞价商品的id 查询订单
			BidOrder bidorder=bidingOrderDao.selectOneBidingOrder(or);
			//如果记录不为空 将它放到竞价记录中
			if(bidorder!=null) {
				bi.setBidOrder(bidorder);
			}
		}
		PageInfo<Bidrecord> pageinfobiddingorder=new PageInfo<>(biddingorder);
		
		
		
		//企业回收
		if(ul==6){
			PageHelper.startPage(pageNum, pageSize);
		}else{
			PageHelper.startPage(1, 5);
		}
		
		List<EnterpriseBack> enterpriseBacks=saleOrderDao.selenterpriseBacks(uid);
		PageInfo<EnterpriseBack> pageinfoenterpriseBacks=new PageInfo<>(enterpriseBacks);
		
		
		
		map.put("pageinfoshopt", pageinfoshopt);
		map.put("pageinfosellorder", pageinfosellorder);
		map.put("pageinfobackorder", pageinfobackorder);
		map.put("pageinfoleaseorder", pageinfoleaseorder);
		map.put("pageinfobiddingorder", pageinfobiddingorder);
		map.put("pageinfoenterpriseBacks", pageinfoenterpriseBacks);
		return map;
	}	
		
	//删除订单
	@Override
	public int deleorder(Item_order item_order) {
		// TODO Auto-generated method stub
		int i=saleOrderDao.deleorder(item_order);
		return i;
	}

	//删除订单商品中间表
	@Override
	public int deleitem_order(Item_order item_order) {
		int i=saleOrderDao.deleitem_order(item_order);
		// TODO Auto-generated method stub
		return i;
	}

	//确定金额
	@Override
	public int assess(Order order) {
		// TODO Auto-generated method stub
		int i=saleOrderDao.assess(order);
		return i;
	}


	@Override
	public int delebackorder(Item_order item_order) {
		// TODO Auto-generated method stub
		int i=saleOrderDao.delebackorder(item_order);
		return i;
	}


	@Override
	public int deleleaseorder(Item_order item_order) {
		// TODO Auto-generated method stub
		int i=saleOrderDao.deleleaseorder(item_order);
		return i;
	}


	@Override
	public int delebiddingorder(Item_order item_order) {
		// TODO Auto-generated method stub		
		if(item_order.getOid()!=null&&item_order.getOid()!=""){
			saleOrderDao.delebiddingorder(item_order);	
		}
		int i=saleOrderDao.delebidorder(item_order);
		return i;
	}

	//支付完成出售商品库存减少
	@Override
	public void stocknumdown(Order order) {
		// TODO Auto-generated method stub
		List<Item_order> item_orders=saleOrderDao.selitem_order(order.getOid());
		for (int i = 0; i < item_orders.size(); i++) {
			SellItemQueryParameters siqp=new SellItemQueryParameters();
			siqp.setCfid(item_orders.get(i).getCfid());
			List<Configurations> conf = sellitemdao.configurations(siqp);
			//库存减少
			Integer stocknum=conf.get(0).getStocknum()-item_orders.get(i).getMun();
			if(stocknum<0){
				stocknum=0;
			}
			Integer cfid=item_orders.get(i).getCfid();
			
			Configurations con=new Configurations();
			con.setCfid(cfid);
			con.setStocknum(stocknum);
			saleOrderDao.stocknumdown(con);
		}
	}


	@Override
	public void kdeit(Order ord) {
		// TODO Auto-generated method stub
		saleOrderDao.kdeit(ord);
	}


	@Override
	public Order findByO(String oid) {
		// TODO Auto-generated method stub
		return saleOrderDao.findByO(oid);
	}


	@Override
	public int deleenterpriseBacks(Integer id) {
		// TODO Auto-generated method stub
		int i=saleOrderDao.deleenterpriseBacks(id);
		return 1;
	}

}
